<?php

namespace app\api\library\promotion\getPromotionUserList;

use app\api\model\MallUser;
use think\facade\Db;

/**
 *
 * User: chenzheng
 * Date: 2023/5/20
 * Time: 13:12
 */
class V
{
    public static function run($param)
    {
        
        $userId = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $where  = [
            ['id', '=', $userId],
        ];
        
        $userInfo = MallUser::where($where)->find();
        if (empty($userInfo)) {
            _outPut(204, '', '用户不存在');
        }
        $page   = $param[ 'page' ];
        $limit  = $param[ 'limit' ];
        $offset = $limit * ($page - 1);
        $where  = [
            ['promotion_id', '=', $userId],
        ];
        $total  = Db::name('mall_user')
            ->where($where)
            ->count();
        $users  = Db::name('mall_user')
            ->where($where)
            ->limit($offset, $limit)
            ->field('id,name,avatar_url,promotion_time')
            ->order(['id' => 'desc'])
            ->select();
        $page   = [
            'total'      => $total,
            'limit'      => $limit,
            'page'       => $page,
            'total_page' => ceil($total / $limit),
        ];
        $res    = [
            'page' => $page,
            'list' => $users,
        ];
        _outPut(200, $res);
    }
}